package com.kly.msg;

import com.google.auth.oauth2.GoogleCredentials;
import com.google.firebase.FirebaseApp;
import com.google.firebase.FirebaseOptions;
import com.google.firebase.messaging.FirebaseMessaging;
import com.google.firebase.messaging.Message;
import com.google.firebase.messaging.Notification;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.ExecutionException;

public class FcmClient {



    String token="dMVi5MDhSje_NnQPQzhxYd:APA91bH3Qv6kJvCizxbzg3_2ncxYArzeDaQaHS2ay8qfHOOzFK2r8LBMiZka0X3ODWFVZ1bO5CdAH_TGdoaFQBRr48F3Y8wxpPfeYswsVzsbi2tXkKcWldEhd5bkVcoJlglwXwIWmITi";
    // The JSON string
    String jsonString = "{\n" +
            "  \"type\": \"service_account\",\n" +
            "  \"project_id\": \"meboo-online-b68e7\",\n" +
            "  \"private_key_id\": \"072e76e982983228c3a733a8c7bc968b7c246385\",\n" +
            "  \"private_key\": \"-----BEGIN PRIVATE KEY-----\\nMIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQDHuOUzHjZZwEzI\\nW/hCF39DhKqUU8OFPzfI7VG/rcGwM1+ZibqTcbrMcH+W+3Bl2fxIEnlZ4Ih+p5Dc\\nGIbvqDjGMQV0a9DJ7Aadybvj0EtEdZaDlAMXXjVRsg86sst1lubOe8iPlwdlEKJO\\npjBrE9jxZpwo/mNK04GkIRV8f9HtNv3aygayigxP5pCE8MyNDYdLFslV8/SHQd3P\\nw15ynUxrrH9j9akQux8B8KmHPXArIjlCzQQ49PETfD6q2/4m+OECXPykyDZlAwZJ\\nqvgCq7JcNYjexXXyE2SZKcGR27r04aZpVcMXq3p2Mb/QA+wpznOHzf8CZEj60KXv\\nLfLYWas9AgMBAAECggEAGSPfif84FTUdFS1qgxtFFEErXa9rkabDBWJGI4wiMOVO\\nl9ub5jpvKpPQo7czz0ZQuNvGdbdLRzIdjG7z8U/qEZyTFLQLnJc+v/boVUVfbBcn\\nIpnrh7H+oTF6N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2vsbk+n2NhojNRrLplBVrThGRoKVI60JkMvxyiTpCak/hLxCXyHhRN3\\nBEKzxSLo8vIYxnLSyDal767skA==\\n-----END PRIVATE KEY-----\\n\",\n" +
            "  \"client_email\": \"firebase-adminsdk-m4bau@meboo-online-b68e7.iam.gserviceaccount.com\",\n" +
            "  \"client_id\": \"106935020118315714458\",\n" +
            "  \"auth_uri\": \"https://accounts.google.com/o/oauth2/auth\",\n" +
            "  \"token_uri\": \"https://oauth2.googleapis.com/token\",\n" +
            "  \"auth_provider_x509_cert_url\": \"https://www.googleapis.com/oauth2/v1/certs\",\n" +
            "  \"client_x509_cert_url\": \"https://www.googleapis.com/robot/v1/metadata/x509/firebase-adminsdk-m4bau%40meboo-online-b68e7.iam.gserviceaccount.com\",\n" +
            "  \"universe_domain\": \"googleapis.com\"\n" +
            "}";
    public String sendMsg() throws IOException {

        InputStream serviceAccount = new ByteArrayInputStream(jsonString.getBytes());

//        InputStream serviceAccount = getClass().getClassLoader().getResourceAsStream("meboo-online.json");

        FirebaseOptions options = new FirebaseOptions.Builder()
                .setCredentials(GoogleCredentials.fromStream(serviceAccount))
                .build();
        FirebaseApp.initializeApp(options);
        System.out.println("Firebase service account key is valid.");
        try {

            final Notification notification = Notification.builder()
                    .setTitle("")
                    .setBody("")
                    .setImage("").build();


            //type 1 回复消息  2普通照片  3私密照片  4普通视频  5私密视频 6wlm喜欢
            //isNotification true 通知栏消息  false应用内消息
            //targetId 目标的userCode
            //imageUrl 对方发送的图片或者视频链接
            // Build a message
            Message message = Message.builder()

                    .putData("type", "10")
                    .putData("isNotification", "false")
//                    .putData("targetId", "12313")
//                    .putData("imageUrl", "http://t15.baidu.com/it/u=1691320874,470802157&fm=224&app=112&f=JPEG?w=500&h=500")
                    .setToken(token)
                    .setNotification(notification)
                    .build();



// Send a message to the device corresponding to the provided
// registration token.
            String response = FirebaseMessaging.getInstance().sendAsync(message).get();
// Response is a message ID string.
            System.out.println("Successfully sent message: " + response);
            return response;
        }catch (Exception e){
            e.printStackTrace();
            return e.getMessage();
        }

    }



    public static void main(String[] args) throws IOException, ExecutionException, InterruptedException {
        new FcmClient().sendMsg();
    }
}

